package cn.com.zhbs.account.service.impl;

import cn.com.zhbs.account.dao.DepartmentMapper;
import cn.com.zhbs.account.dao.MenuMapper;
import cn.com.zhbs.account.dao.RoleMapper;
import cn.com.zhbs.account.entitys.Menu;
import cn.com.zhbs.account.entitys.Node;
import cn.com.zhbs.account.service.MenuService;
import cn.com.zhbs.account.util.TreeRecursion;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 菜单管理实现层
 * @author: 陈治杰
 * @create: 2021-08-08 13:12
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    MenuMapper menuMapper;
    @Resource
    DepartmentMapper departmentMapper;
    @Resource
    RoleMapper roleMapper;

    @Override
    public JSONArray menuList(JSONObject jsonObject) {
        jsonObject.put("dids",departmentMapper.findDIDByAID(jsonObject));
        List<JSONObject> menus=menuMapper.findMenuByDID(jsonObject);
        Map<String, Node> nodes = new HashedMap();
        for(int i=0;i<menus.size();i++){
            //模拟数据库存储树结构
            nodes.put(""+i+1,new Gson().fromJson(menus.get(i).toString(),Node.class));

        }
        JSONArray menu= TreeRecursion.getNodeJson("0",nodes);
        return TreeRecursion.sortList(menu);
    }

    @Override
    public Map<String,Object> addMenu(JSONObject jsonObject) {
        if(menuMapper.isResourceExist(jsonObject)>0){
            return ResultFactory.failure("0","该模块已存在",null).display();
        }
        jsonObject.put(Menu.order_num.toString(),menuMapper.findOrderNumByResourceID(jsonObject));
        menuMapper.addResource(jsonObject);
        return ResultFactory.success().display();
    }

    @Override
    public void editMenu(JSONObject jsonObject) {
        menuMapper.updateResource(jsonObject);
    }

    @Override
    public JSONObject menuDetail(JSONObject jsonObject) {
        jsonObject.put("dids",departmentMapper.findDIDByAID(jsonObject));
        return menuMapper.findResourceByResourceID(jsonObject);
    }
}
